import { apiBaseUrl, imgBaseUrl, defaultImg } from "../../../utils/config";
import {promiseRequest,getOpenId} from "../../../utils/index";
import { collectFormIds, saveFormIds } from "../../../utils/index";
const app = getApp();
var formIdlist = [];
Page({
  data: {
    actshareinfo:{},
    ljtj:'',
    imgList:[],
    imgBaseUrl: imgBaseUrl,
    isshare:false,
    isreceive:false,
    actreceive:{},
    backImg: '',
    shareTitle: ''
  },
//获取缓存中存储的活动信息
  onLoad(options) {
  let actId = '';
  if(options.scene){
    actId = decodeURIComponent(options.scene);
  }else{
    actId = options.actId;
  }
  //根据openid和活动Id查询领取信息
  //getToken().then(() => {
    //查询活动信息
    this.getactshareinfo(actId);
    let openid = getOpenId();
    this.getactreceive(actId, openid);
  //});
},
onShow(){
},
//页面销毁时提交收集到的formid
onUnload() {
  saveFormIds(formIdlist);
  formIdlist = [];
},
getactshareinfo(actId){
  let requestObj = {
    url: '/wxa/actshare/getcacheShareById',
    method: 'post',
    dataobj: {actId: actId },
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      let actshareinfo = res.data;
      let rewardDetail = actshareinfo.rewardDetail;
      rewardDetail = rewardDetail.split(",");
      rewardDetail = rewardDetail == "" ? [] : rewardDetail;
      let backImg = '';
      if (actshareinfo.backImg == '' || actshareinfo.backImg == null) {
        backImg = defaultImg
      } else {
        backImg = imgBaseUrl + "/" + actshareinfo.backImg
      }
      this.setData({
        actshareinfo: actshareinfo,
        imgList: rewardDetail,
        backImg: backImg,
        shareTitle: "免费领取" + actshareinfo.rewardName + "(价值:" + actshareinfo.rewardPrice + "元)"
      })
      //助力个数
      let helpNumber = actshareinfo.helpNumber ? actshareinfo.helpNumber : 'X';
      if (actshareinfo.helpWayCode == 0) {
        this.setData({
          ljtj: '邀请好友助力达' + helpNumber + '次即可领奖',
        })
      } else if (actshareinfo.helpWayCode == 1) {//助力前N名
        this.setData({
          ljtj: '助力排名前' + helpNumber + '即可领奖',
        })
      }
    }
  }).catch((errMsg) => {
  });
},
share(){
  this.setData({
    isshare: true
  })
},
onShareAppMessage() {
  return {
    title: this.data.shareTitle,
    path: '/pages/compent/shareHelp/shareHelp?actId=' + this.data.actshareinfo.id,
    imageUrl: this.data.backImg
  }
},
hideModal(){
  this.setData({
    isshare: false
  })
},
jump(e) {
  var page = e.currentTarget.dataset.page;
  wx.navigateTo({
    url: page
  });
},
//根据openid和活动actId查询用户领取信息
 getactreceive(actId,openid) {
   let requestObj = {
     url: '/wxa/actreceive/detailCache',
     method: 'post',
     dataobj: { actId: actId, openid: openid},
   };
   promiseRequest(requestObj).then((res) => {
     if (res.code == 200) {
       if (res.data) {
         this.setData({
           isreceive: true,
           actreceive: res.data
         })
       }
     }
   }).catch((errMsg) => {
   });
},
//插入用户领取活动奖品的信息记录
saveactreceive(param){
  let requestObj = {
    url: '/wxa/actreceive/save',
    method: 'post',
    dataobj: param,
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      wx.navigateTo({
        url: "../shareReceive/shareReceive?lqropenid=" + param.openid + "&actId=" + param.actId
      });
    }
  }).catch((errMsg) => {
  });
},
//用户参加活动需获取用户信息
getUserInfo(e) {
  if (e.detail.userInfo) {
    let userinfo = e.detail.userInfo;
    let actId = this.data.actshareinfo.id;//活动主键
    let znum = this.data.actshareinfo.helpNumber;//活动需要的总助力数
    let grantWayCode = this.data.actshareinfo.grantWayCode;
    let openid = getOpenId();
    let param = { actId: actId,openid:openid, grantWayCode:grantWayCode,znum:znum,nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl, gender: userinfo.gender, language: userinfo.language }
    //用户按了允许授权按钮
    wx.checkSession({
      success: () => {
        this.saveactreceive(param);
      },
      fail: () => {
        // 重新授权
        // getToken().then(() => {
        //   this.saveactreceive(param);
        // })
      }
    })
  }else{
    console.log("拒绝决绝");
  }
},
//查看更多优惠活动
moreshare(e){
  collectFormIds(e, formIdlist);
  wx.switchTab({
    url: '../../tabar/index/index',
  })
},
//点击我要领取收集fomid
wylq(e){
  console.log('单击我要领取收集fomid');
  collectFormIds(e, formIdlist);
},
//点击好友帮忙收集fomid
hybm(e) {
  console.log('单击好友帮忙收集fomid');
  collectFormIds(e, formIdlist);
  wx.navigateTo({
    url: '../shareReceive/shareReceive?actId='+this.data.actshareinfo.id+'&lqropenid='+this.data.actreceive.openid,
  })
},
//查看助力信息
helpinfo(){
  wx.navigateTo({
    url: '../helper/helper?actId=' + this.data.actshareinfo.id+'&receiveId=' + this.data.actreceive.id
  })
}
})